package ui;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import manager.AgentManagerMIDlet;


import java.util.Vector;

public class GetDestinationEntryScreen extends Form implements CommandListener{
    private final AgentManagerMIDlet midlet;
    //private final Form fmMain;
    //private final TextBox tbAction;
    private final Command exitCommand;
    private final Command myCommand1;
    private final Command myCommand2;
    private final Command confirmCommand;
//    private DateField timeEntry;
    private final Display display;
    private ChoiceGroup cgAgentFinal;
    private TextField tfNum;
    private Vector dests;
    private int currentDestIndex;
    private int numDestinations;
    private Vector startTimes;
    private Vector endTimes;
    private String message;
    private String type;
    
    GetDestinationEntryScreen(AgentManagerMIDlet midlet, 
    		String type,
    		Display display, 
    		int numDestinations, 
    		int currentDestIndex,
    		Vector dests, 
    		Vector startTimes,
    		Vector endTimes,
    		String message)
    {
        super("Destination entry");
        this.type = type;
        this.message = message;
        this.display = display;
        this.midlet = midlet;
        this.startTimes = startTimes;
        this.endTimes = endTimes;
        this.numDestinations = numDestinations;
        //this.currentDestIndex = currentDestIndex;
        this.dests=dests;
        if(currentDestIndex==0){
        	this.dests = new Vector(numDestinations);
        }
        
        myCommand1 = new Command("myCommand1", Command.SCREEN, 2);
        myCommand2 = new Command("myCommand2", Command.SCREEN, 3);
        confirmCommand = new Command("Confirm", Command.SCREEN,1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
		tfNum = new TextField("Enter destination : ", "socket://localhost:5000" ,200,TextField.ANY);
		this.append(tfNum);
        this.addCommand(confirmCommand);
        this.addCommand(exitCommand);
        this.addCommand(myCommand1);
        this.addCommand(myCommand2);
        this.setCommandListener(this);
    }


    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            midlet.exitRequested();
        }else if(c==confirmCommand){
        	System.out.println("ConfirmCommand");
        	if(++currentDestIndex<(numDestinations)){
        		//Go to next screen destination entry screen
        	//System.out.println("YO YO YO");
	        	dests.addElement(tfNum.getString());
	        	GetDestinationEntryScreen getDestinationEntryScreen = 
	        		new GetDestinationEntryScreen(midlet, type, display, 
	        	    		numDestinations, currentDestIndex, dests,startTimes,endTimes,message);
	        	display.setCurrent(getDestinationEntryScreen);
        	}else{
        		System.out.println("midlet : " + midlet.toString());
        		System.out.println("display : " + display.toString());
        		System.out.println("dests : " + dests.toString());
	        	dests.addElement(tfNum.getString());
        		//Final Screen
        		GetDestinationFinalScreen getDestinationFinalScreen = 
        			new GetDestinationFinalScreen(midlet,type,display,dests,startTimes,endTimes,message);
        		System.out.println("Should be after this");
        		display.setCurrent(getDestinationFinalScreen);
        	}
        }
    }
}
